package pl.prv.polanie.client.dto.comparators;

import java.util.Comparator;

import pl.prv.polanie.client.dto.CSVSubjectEventDTO;

/**
 * 
 * 
 * @author Janusz Prokulewicz
 * Created: Jun 10, 2008
 */
public class Comparators {
	
	
	@SuppressWarnings("unchecked")
	public static Comparator getComparatorCSVSubjectEvent(final int colIndex) {
		Comparator cmp = new Comparator() {
			public int compare(Object o1, Object o2) {

//			    final int BEFORE = -1;
			    final int EQUAL = 0;
//			    final int AFTER = 1;
			    
			    CSVSubjectEventDTO obj1;
			    CSVSubjectEventDTO obj2;
			    if (o1 instanceof CSVSubjectEventDTO && o2 instanceof CSVSubjectEventDTO) {

					obj1 = (CSVSubjectEventDTO) o1;
					obj2 = (CSVSubjectEventDTO) o2;
					
					//this optimization is usually worthwhile, and can
					//always be added
					if ( obj1 == obj2 ) return EQUAL;

					//primitive numbers follow this form
					if (colIndex == CSVSubjectEventDTO.SORT_CONST__ID) {
						Long l1 = new Long(obj1.getId());
						Long l2 = new Long(obj2.getId());
						return l1.compareTo(l2);
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__SUBJECT) {
						return obj1.getSubject().compareTo(obj2.getSubject());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__ROOM) {
						return obj1.getRoom().compareTo(obj2.getRoom());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__SUBGROUP) {
						return obj1.getSubgroup().compareTo(obj2.getSubgroup());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__LECTURER) {
						return obj1.getLecturer().compareTo(obj2.getLecturer());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__CALENDAR_DAY) {
						return obj1.getCalendarDay().compareTo(obj2.getCalendarDay());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__START_TIME) {
						return obj1.getStartTime().compareTo(obj2.getStartTime());
					} else if (colIndex == CSVSubjectEventDTO.SORT_CONST__DURATION) {
						return obj1.getDuration().compareTo(obj2.getDuration());
					}
					
				};
				return EQUAL;
			}
		};

		return cmp;
	}

	


}
